<?php

namespace App\Recharge\Api;

use App\Models\InterfaceConfiguration;
use App\Recharge\Bean\ApiNotifyDataBean;
use App\Recharge\Bean\BasicInformation;
use Illuminate\Http\Request;

class Xunyin extends AbstractApi
{

    /**
     * @inheritDoc
     */
    public function rechargeData(BasicInformation $bean): array
    {
        $array = [
            'cpid'          => $bean->getSupplierNum(), //商户号
            'gamegoodid'    => $bean->getGoodsId(), //商品编号id,
            'createtime'    => date('YmdHis'),
            'account'       => $bean->getAccount(), //要充值的用户
            'orderid'       => $bean->getOrderNo(), //订单号
            'buynum'        => $bean->getRechargeNum() * $bean->getStartCharging(),
        ];
        $array['sign'] = md5(urldecode( http_build_query( $array ) ) . $bean->getSecretKey());
        $array['buyerIp'] = $bean->getIp();
        $array['returnurl'] = $bean->getNotifyUrl(); //异步回调地址

        return $array;
    }

    /**
     * @param InterfaceConfiguration $api
     * @param Request $request
     * @return ApiNotifyDataBean
     */
    public function apiNotifyLogic(InterfaceConfiguration $api, Request $request): ApiNotifyDataBean
    {
        $status = $request->input('status', '');
        $order_sn = $request->input('id', '');
        return $this->apiNotifyData($order_sn, $status)->setExecute(in_array($status, ['SUCCESS', 'FAIL']))->setRechargeSuccess($status == 'SUCCESS');
    }

    /**
     * @return mixed
     */
    public function apiNotifySuccess(): mixed
    {
        return response('OK');
    }
}
